import com.sun.xml.internal.ws.addressing.WsaActionUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BankAccountManager {

    static Scanner scanner = new Scanner(System.in);

    // 创建一个集合用来装添加的账户信息
    private static List<BankAccount> bankAccountList = new ArrayList<>();

    public static void main(String[] args) {
        // 加载数据
        loadData();
        while (true) {
            System.out.println("1:添加账户、2:查询账户、3:删除账户、4:转账、5:机器人、6：退出");
            int i = scanner.nextInt();
            if (i == 1) {
                // 实现添加添加的账户的逻辑
                addAccount();
            } else if (i == 2) {
                getAccountList();
            } else if (i == 3) {
                removeAccount();
            } else if (i == 4) {
                transfer();
            } else if (i == 5) {
                // 机器人的对话
                robotSession();
            } else if (i == 6) {
                saveFile();
                System.out.println("bye bye.");
                System.exit(-1);
            }
        }
    }

    private static void robotSession() {
        boolean isExits = true;
        System.out.println("进入聊天模式");
        while (isExits) {
            System.out.println("请输入要发送给机器人的内容(输入exit表示退出！！)");
            String msg = scanner.next();
            if ("exit".equals(msg)) {
                System.out.println("退出聊天模式");
                isExits = false;
            }else{
                // 1.要把这个内容发送给机器人,获取结果
                String respMsg = GptUtils.getMsg(msg);

                // 2.输出结果
                System.out.println("【机器人】" + respMsg);
            }
        }
    }

    private static void transfer() {
        System.out.println("输入转入账户:");
        String inAccount = scanner.next();

        System.out.println("输入转出账户:");
        String outAccount = scanner.next();

        System.out.println("输入金额:");
        double money = scanner.nextDouble();

        // 检查转入账户是否存在
        BankAccount inBankAccount = getAccount(inAccount);
        if (inBankAccount == null) {
            System.out.println("转入账号不存在");
            return;
        }

        // 检查转出账号是否存在
        BankAccount outBankAccount = getAccount(outAccount);
        if (outBankAccount == null) {
            System.out.println("转出账号不存在");
            return;
        }

        // 检查金额是否合理
        if (money <= 0) {
            System.out.println("金额必须要大于0");
            return;
        }

        // 检查余额是否充足
        if (outBankAccount.getBalance() < money) {
            System.out.println("账户余额不足");
            return;
        }

        // 给转出账户扣钱
        outBankAccount.out(money);
        // 给转入账户加钱
        inBankAccount.in(money);

        System.out.println("转账完成");
    }

    private static BankAccount getAccount(String inAccount) {
        for (BankAccount bankAccount : bankAccountList) {
            if (bankAccount.getAccount().equals(inAccount)) {
                return bankAccount;
            }
        }
        return null;
    }

    public static void loadData() {
        ObjectInputStream ips = null;
        try {
            ips = new ObjectInputStream(new FileInputStream("./bankAccount.db"));
            bankAccountList = (List<BankAccount>) ips.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ips != null) {
                try {
                    ips.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 持久化
    private static void saveFile() {
        ObjectOutputStream write = null;
        try {
            write = new ObjectOutputStream(new FileOutputStream("./bankAccount.db"));
            write.writeObject(bankAccountList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (write != null) {
                try {
                    write.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private static void removeAccount() {
        System.out.println("请输入要删除的账户");
        String account = scanner.next();
        boolean isDelete = false; // 默认是删除失败的
        for (BankAccount bankAccount : bankAccountList) {
            if (bankAccount.getAccount().equals(account)) {
                bankAccountList.remove(bankAccount);
                isDelete = true; // 修改删除成功
                System.out.println("删除【" + account + "】成功");
                return;
            }
        }
        if (!isDelete) {
            System.out.println("【" + account + "】不存在");
        }
    }

    private static void getAccountList() {
        for (BankAccount bankAccount : bankAccountList) {
            System.out.println(bankAccount);
        }
    }

    private static void addAccount() {

        // 1.用户输入相关信息
        System.out.println("请输入账户");
        String account = scanner.next();

        System.out.println("请输入余额");
        double balance = scanner.nextDouble();

        // 2.封装
        BankAccount bankAccount = new BankAccount();
        bankAccount.setAccount(account);
        bankAccount.setBalance(balance);

        // 3.添加到集合中
        bankAccountList.add(bankAccount);
        System.out.println("添加【" + account + "】完成");
    }
}
